package br.com.speedprevweb.bean;

import br.com.speedprevweb.dao.TipoRegimeDao;
import br.com.speedprevweb.dao.impl.TipoRegimeDaoImpl;
import br.com.speedprevweb.model.TipoRegime;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "tipoRegimeBean")
@SessionScoped
public class TipoRegimeBean implements Serializable {

  private TipoRegime tipoRegime = new TipoRegime();
  private List<TipoRegime> tiposRegime = new ArrayList();

  public TipoRegimeBean() {
    tiposRegime = new TipoRegimeDaoImpl().listarTodos();
    tipoRegime = new TipoRegime();
  }

  public TipoRegime getTipoRegime() {
    return tipoRegime;
  }

  public void setTipoRegime(TipoRegime tipoRegime) {
    this.tipoRegime = tipoRegime;
  }

  public List<TipoRegime> getTiposRegime() {
    return tiposRegime;
  }

  public void setTiposRegime(List<TipoRegime> tiposRegime) {
    this.tiposRegime = tiposRegime;
  }

  public String descricaoTipoRegime() {
    return this.tipoRegime.getDsTipoRegime();
  }

  public String incluirTipoRegime() {
    tipoRegime = new TipoRegime();
    return "TipoRegimeDetalhe";
  }

  public String preparaAlterarTipoRegime() {
    return "TipoRegimeDetalhe";
  }

  public String excluirTipoRegime() {
    TipoRegimeDao tpRegimeDao = new TipoRegimeDaoImpl();
    tpRegimeDao.excluir(tipoRegime);
    tiposRegime = new TipoRegimeDaoImpl().listarTodos();
    tipoRegime = new TipoRegime();
    return "TipoRegime";
  }

  public String salvar() {
    if (tipoRegime.getIdTipoRegime() == 0) {
      incluir();
    } else {
      alterar();
    }
    return "TipoRegime";
  }

  public void incluir() {
    TipoRegimeDao tpRegimeDao = new TipoRegimeDaoImpl();
    tpRegimeDao.salvar(this.tipoRegime);
    tiposRegime = new TipoRegimeDaoImpl().listarTodos();
    tipoRegime = new TipoRegime();
  }

  public void alterar() {
    TipoRegimeDao tpRegimeDao = new TipoRegimeDaoImpl();
    tpRegimeDao.alterar(tipoRegime);
  }

  public String cancelar() {
    return "TipoRegime";
  }

  private void atualizarDataTable() {
    tiposRegime = new TipoRegimeDaoImpl().listarTodos();
    tipoRegime = new TipoRegime();
  }

  public int totalRegistros() {
    return tiposRegime.size();
  }
}